<template>

  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="员工名称" prop="employeeName">
        <el-input v-model="queryParams.employeeName" placeholder="请输入员工名称" clearable style="width: 200px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5" v-if="false">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row>

    <el-table border v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="55">
        <template #default="{ $index }">
          {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="员工名称" align="" prop="employeeName" :show-overflow-tooltip="true" />
      <el-table-column label="员工头像" width="100" align="center" prop="employeeAvatar">
        <template #default="scope">
          <el-image style="width: 50px; height: 50px;" :src="scope.row.employeeAvatar"
            :preview-src-list="[scope.row.employeeAvatar]" preview-teleported>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="投放形式" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.isPublish" link type="primary" icon="edit"
            @click="handleUpdate(scope.row)">投放形式</el-button>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column label="状态" align="center" prop="isPublish">
        <template #default="scope">
          <span v-if="scope.row.isPublish == true">已发布</span>
          <span v-if="scope.row.isPublish == false">未发布</span>
        </template>
      </el-table-column>


      <el-table-column label="操作人员" align="center" prop="updateUserName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" v-if="!scope.row.isPublish"
            @click="handlePublish(scope.row)">发布</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.pageSize" @pagination="getList" />





    <!-- 投放 -->
    <el-dialog title="医生竞价排名投放详情" v-model="open2" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="投放范围" prop="scope">
              <el-select v-model="form.scope" placeholder="">
                <el-option v-for="item in scopeList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-input v-model="form.startDate" placeholder="请输入开始日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="列表位置" prop="sort">
              <el-input value="用户端医疗机构界面医生列表" placeholder="请输入列表位置" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-input v-model="form.endDate" placeholder="请输入结束日期" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="位置排名" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排名" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>


    <!-- 发布 -->
    <el-dialog :title="title" v-model="open" width="780px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker class="flex-1" v-model="form.startDate" value-format="YYYY-MM-DD" type="date"
                range-separator="To" start-placeholder="Start date" end-placeholder="End date" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker class="flex-1" v-model="form.endDate" value-format="YYYY-MM-DD" type="date"
                range-separator="To" start-placeholder="Start date" end-placeholder="End date" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布排名" prop="scope">
              <el-select v-model="form.sort" placeholder="请选择排名">
                <el-option v-for="dict in sortList" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布范围" prop="scope">
              <el-select v-model="form.scope" placeholder="请选择范围">
                <el-option v-for="dict in scopeList" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>




  </div>
</template>

<script setup name="Notice">


import {
  advertisementRuleApirule
} from "@/api/backAdvertisementRule/backAdvertisementRule.js";


import {
  advertisementEmployeeApiPublish,
  listNotice,
  delNotice,
} from "@/api/backAdvertisementEmployee/backAdvertisementEmployee.js";

import {
  advertisementEmployeeApisupervisedetail,
} from "@/api/backAdvertisementEmployee/backAdvertisementEmployee";

import { onMounted } from "vue";

const { proxy } = getCurrentInstance();
const { scope_status } = proxy.useDict(
  'scope_status'
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


let sortList = [
  { label: "第一位", value: 1 },
  { label: "第二位", value: 2 },
  { label: "第三位", value: 3 },
  { label: "第四位", value: 4 },
]

let scopeList = [
  { label: "所在地一公里内发布", value: 1 },
  { label: "所在区县内发布", value: 2 },
  { label: "所在市内发布", value: 3 },
  { label: "所在省内发布", value: 4 },
  { label: "所在全国内发布", value: 5 },
]

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
  },
  rules: {
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商家列表 */
function getList() {
  loading.value = true;
  listNotice(queryParams.value).then((response) => {
    noticeList.value = response.records;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}


/* 发布操作 */
function handlePublish(row) {
  reset()
  advertisementRuleApirule(4).then((re) => {
    if (re.sortList) {
      sortList = re.sortList.map(val => sortList.find(item => item.value === val));
    }
    scopeList = re.scopeList.map((val) => scopeList.find(i => val == i.value));
  })

  open.value = true;
  title.value = "发布广告";
  form.value.advertisementEmployeeId = row.advertisementEmployeeId
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      advertisementEmployeeApiPublish(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    }
  });
}




/** 删除按钮操作 */
function handleDelete(row) {
  const advertisementEmployeeIds = row.advertisementEmployeeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除商家编号为"' + advertisementEmployeeIds + '"的数据项？')
    .then(function () {
      return delNotice(advertisementEmployeeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}


let open2 = ref(false)
function handleUpdate(row) {
  reset();
  const advertisementEmployeeId = row.advertisementEmployeeId || ids.value;
  advertisementEmployeeApisupervisedetail(advertisementEmployeeId).then((response) => {
    form.value = response;
    open2.value = true;
    title.value = "投放形式";
  });
}


/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.advertisementEmployeeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}



getList();


</script>
